﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shop.Model
{
    //internal class CustomDateContractResolver : DefaultContractResolver
    //{
    //    protected override JsonContract CreateContract(Type objectType)
    //    {
    //        //JsonContract contract = base.CreateContract(objectType);
    //        //bool b = objectType == typeof(DateTime);
    //        //if (b)
    //        //{
    //        //    contract.Converter = new ZerosIsoDateTimeConverter("yyyy-MM-dd hh:mm:ss", "0000-00-00 00:00:00");
                
    //        //}
    //        //return contract;


    //        Newtonsoft.Json.Converters.IsoDateTimeConverter DateTimeConverter 
    //            = new Newtonsoft.Json.Converters.IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd hh:mm:ss", DateTimeStyles=  };

    //    }
    //}

    public class MyIsoDateTimeConverter: IsoDateTimeConverter
    {
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {

            if (reader.Value?.ToString()?.StartsWith ("0000-00-00") == true)
            {
                if (objectType.IsNullableType())
                {
                    return null;
                }

                return DateTime.MinValue;
            }
            else
            {
                return base.ReadJson(reader, objectType, existingValue, serializer);
                //return Convert.ToDateTime(reader.Value);
            }

        }
    }

}
